%option noyywrap
%{

%}

INTCON     [0-9]+
IDENFR     [a-zA-Z_][a-zA-Z0-9_]*
STRCON     \"([^\"\\]|\\.)*\" 


%%

"main"              { printf("MAINTK main\n"); }
"const"             { printf("CONSTTK const\n");  }
"int"               { printf("INTTK int\n");} 
"getint"            { printf("GETINTTK getint\n");}
"printf"            { printf("PRINTFTK printf\n");  }
"return"            { printf("RETURNTK return\n");  }
"while"             { printf("WHILETK while\n"); }
"if"                { printf("IFTK if\n"); }
"else"              { printf("ELSETK else\n");  }
"void"              { printf("VOIDTK void\n"); }
"break"             { printf("BREAKTK break\n"); }
"continue"          { printf("CONTINUETK continue\n");  }

"="                 { printf("ASSIGN =\n"); }
"=="                { printf("EQL ==\n");}
"!="                { printf("NEQ !=\n"); }
"<"                 { printf("LSS <\n"); }
"<="                { printf("LEQ <=\n");}
">"                 { printf("GRE >\n"); }
">="                { printf("GEQ >=\n"); }
"&&"                { printf("AND &&\n");}
"||"                { printf("OR ||\n");}
"!"                 { printf("NOT !\n"); }
"+"                 { printf("PLUS +\n");}
"-"                 { printf("MINU -\n"); }
"*"                 { printf("MULT *\n"); }
"/"                 { printf("DIV /\n"); }
"%"                 { printf("MOD %%\n");}
"("                 { printf("LPARENT (\n"); }
")"                 { printf("RPARENT )\n");}
"{"                 { printf("LBRACE {\n"); }
"}"                 { printf("RBRACE }\n"); }
"["                 { printf("LBRACK [\n"); }
"]"                 { printf("RBRACK ]\n"); }
";"                 { printf("SEMICN ;\n"); }
","                 { printf("COMMA ,\n"); }

{INTCON}            { printf("INTCON %s\n",yytext); }
{IDENFR}            { printf("IDENFR %s\n",yytext); }
{STRCON}            { printf("STRCON %s\n", yytext); }

[ \t\r\n]+          ;   // 忽略空白符
.                   { printf("TOKEN: UNKNOWN (%s)\n", yytext); }
"//"[^"\n"]*       ;   // 跳过单行注释
%%

int main(int argc, char *argv[]) {
    while (yylex()) {
        // 处理每个词法单元
    }
    return 0;
}
